<!DOCTYPE html>
<html lang="zh-CN">
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<title>SwiftJSON</title>
		<script src="js/jquery-1.12.4.min.js"></script>

		<script type="text/javascript">

			var prSelf = "self.";
			
			$(function() {
				// 1.清空
				$('#clearBtn').click(function() {
					$("#inText").val("");
				});

				// 2.复制
				$('#copyBtn').click(function() {
					var tView = $("#outText");
					if (tView.val().length < 6) {
						return;
					}

					tView.select();
					document.execCommand("copy");
				});

				// 3.SelfBtn
				$('#selfBtn').click(function() {
					var temp = $("#selfBtn").val();
					if (temp == "不要Self") {
						$("#selfBtn").val("加上Self");
						prSelf = "";
					} else {
						$("#selfBtn").val("不要Self");
						prSelf = "self.";
					}
				});

				// 4.转换
				$('#foramteBtn').click(function() {
					$("#outText").val("");

					// 1.获取全部内容
					ApplyLineBreaks("inText");
					var allString = document.getElementById("inText").value;

					if (allString.length <= 0) {
						alert("请输入内容")
						return;
					}

					// 2.分割字符串，每一行，--> 去掉前后空格 --> 去掉注释
					// https://www.jb51.net/article/109522.htm
					var allArray = allString.split("\n").map(function(item) {
						return $.trim(item);
					}).filter(function(item) {
						return item.indexOf("/") != 0;
					});
						
					// 3.解析数据
					for (var line of allArray) {
						if (line.length <= 0) {
							continue;
						}

						if (line.length < 6) { 
							var temp = "// 解析失败行: " + line;
							$("#outText").val($("#outText").val() + "\n" + temp);
							continue;
						}

						if (line.search(":") == -1) {
							formateLineBB(line);
							continue;
						}
						formateLineAA(line);
					}

					$("#outText").val($.trim($("#outText").val()));
					formateLineCC()
				});
			});

			// 将输入框的内容，格式化一下，去掉前后空格和无意义的换行
			function formateLineCC() {
				// 1.获取全部内容
				ApplyLineBreaks("inText");
				var allString = document.getElementById("inText").value;

				// 2.分割字符串，每一行，--> 去掉前后空格 --> 去掉注释
				// https://www.jb51.net/article/109522.htm
				allString = allString.split("\n").map(function(item) {
					return $.trim(item);
				}).filter(function(item) {
					return item.length >= 1;
				}).join("\n");

				$("#inText").val(allString);
			}

			// 含有:的情况
			function formateLineAA(line) {
				if (line.indexOf(":") == -1) {
					var temp = "// 解析失败行: " + line;
					$("#outText").val($("#outText").val() + "\n" + temp);
					return;
				}

				// let name: String = "123"
				// let name: String?
				// let name: String? = nil
				// let name: [Int] = []
				// let name: [String: Int] = [:]
				// let name: Class?
				try {
					// 1.以:分割分组
					var arraAA = line.split(":");
					var letVar = arraAA[0].split(" ")[0];
					if (letVar != "var" && letVar != "let") {
						var temp = "// 解析失败行: " + line;
						$("#outText").val($("#outText").val() + "\n" + temp);
						return;
					}
					// 1.1 拿到属性名
					var pName = arraAA[0].split(" ")[1];
					// 1.2 判断属性类型
					var pType = "";
					var tName = $.trim(arraAA[1]);

					if (tName.indexOf("String") == 0) {
						pType = ".stringValue";
					} else if (tName.indexOf("Int") == 0) {
						pType = ".intValue";
					} else if (tName.indexOf("Bool") == 0) {
						pType = ".boolValue";
					} else if (tName.indexOf("Double") == 0) {
						pType = ".doubleValue";
					} else if (tName.indexOf("Float") == 0) {
						pType = ".floatValue";
					} else if (tName.indexOf("[") != -1 && tName.indexOf("]") == -1) {
						pType = " // 😝不知道咋解析，记得手动处理我😝";
					} else if (tName.indexOf("[") == 0) {
						pType = ".arrayValue // 😝需要检查我";
					} else {
						pType = " // 😝不知道咋解析，记得手动处理我😝";
					}

					var temp = prSelf + pName + " = " + "json[\"" + pName + "\"]" + pType;
					$("#outText").val($("#outText").val() + "\n" + temp);

				} catch(err) {
					console.log("😝BB捕获到异常: ", err.message);
					var temp = "// 解析失败行: " + line;
					$("#outText").val($("#outText").val() + "\n" + temp);
				}
			}

			// 没有:的情况，比如`let name = ""`
			function formateLineBB(line) {
				if (line.indexOf("=") == -1) {
					var temp = "// 解析失败行: " + line;
					$("#outText").val($("#outText").val() + "\n" + temp);
					return;
				}

				// let name = ""
				// let name = "HouWan"
				// let age = 0
				// let age = 12.0
				// let age = 1
				try {
					// 1.以=分割分组
					var arraAA = line.split("=");
					var letVar = arraAA[0].split(" ")[0];
					if (letVar != "var" && letVar != "let") {
						var temp = "// 解析失败行: " + line;
						$("#outText").val($("#outText").val() + "\n" + temp);
						return;
					}
					// 1.1 拿到属性名
					var pName = arraAA[0].split(" ")[1];
					// 1.2 判断属性类型
					var pType = "";
					var tName = $.trim(arraAA[1]);
					if (tName.indexOf("\"") != -1) {
						pType = ".stringValue";
					} else if (tName.indexOf("true") != -1 || tName.indexOf("false") != -1) {
						pType = ".boolValue";
					} else if (tName.indexOf(".") != -1) {
						pType = ".doubleValue";
					} else {
						pType = ".intValue";
					}
					var temp = prSelf + pName + " = " + "json[\"" + pName + "\"]" + pType;
					$("#outText").val($("#outText").val() + "\n" + temp);

				} catch(err) {
					console.log("😝BB捕获到异常: ", err.message);
					var temp = "// 解析失败: " + line;
					$("#outText").val($("#outText").val() + "\n" + temp);
				}
			}

			// https://www.icode9.com/content-1-289756.html
			function ApplyLineBreaks(strTextAreaId) {
				var oTextarea = document.getElementById(strTextAreaId);
				if (oTextarea.wrap) {
					oTextarea.setAttribute("wrap", "off");
				} else {
					oTextarea.setAttribute("wrap", "off");
					var newArea = oTextarea.cloneNode(true);
					newArea.value = oTextarea.value;
					oTextarea.parentNode.replaceChild(newArea, oTextarea);
					oTextarea = newArea;
				}

				var strRawValue = oTextarea.value;
				oTextarea.value = "";
				var nEmptyWidth = oTextarea.scrollWidth;
				var nLastWrappingIndex = -1;
				for (var i = 0; i < strRawValue.length; i++) {
					var curChar = strRawValue.charAt(i);
					if (curChar == ' ' || curChar == '-' || curChar == '+') {
						nLastWrappingIndex = i;
					}
					oTextarea.value += curChar;
					if (oTextarea.scrollWidth > nEmptyWidth) {
						var buffer = "";
						if (nLastWrappingIndex >= 0) {
							for (var j = nLastWrappingIndex + 1; j < i; j++)
								buffer += strRawValue.charAt(j);
							nLastWrappingIndex = -1;
						}
						buffer += curChar;
						oTextarea.value = oTextarea.value.substr(0, oTextarea.value.length - buffer.length);
						oTextarea.value += "\n" + buffer;
					}
				}
				oTextarea.setAttribute("wrap", "");
			}
	
		</script>

	<style type="text/css">
		.textView {
			height: 300px;
            width: 800px;
			font-size: 15px;
			color: #27342b;
		}
		#outText {
			color: #274d3d;
		}
		input {
			height: 25px;
            width: 80px;
		}
		.spView {
			height: 6px;
		}
		.tempCode {
			border: 1px solid blueviolet;
			width: 770px;
			font-size: 14px;
			color: #1b315e;
			height: 300px;
			padding: 5px 15px 5px 15px;
		}
	</style>

	</head>
	<body>
		<h1>快捷处理SwiftJSON</h1>
		<p>在下面输入或者粘贴属性：</p>
		<textarea class="textView" id="inText"></textarea>

		<br>
		<input type="button" value="转换" id="foramteBtn">
		<input type="button" value="清空" id="clearBtn">
		<input type="button" value="复制结果" id="copyBtn">
		<input type="button" value="不要Self" id="selfBtn">
		<br>
		<div class="spView"></div>

		<textarea readonly="readonly" class="textView" id="outText">等待结果...</textarea>
		<br>
		<p>1. 对于解析到的`12.00`的浮点型都是转为`doubleValue`</p>
		<p>2. 除非明确浮点型是`Float`，才能解析为`floatValue`</p>
		<p>3. 下面的写法全部能解析，无论你是否加了空格，也不管是`let`还是`var` </p>
<div class="tempCode">
let name: String = "123"<br>
let name: String?<br>
let name: String? = nil<br>
let name: [Int] = []<br>
let name: [String: Int] = [:]<br>
let name: Class?<br>
let name = ""<br>
let name = "HouWan"<br>
let age = 0<br>
let age = 12.0<br>
let age = 1<br>
var have = true<br>
var hit = false<br>
var xx: Bool?<br>
var xx: Bool = true<br>
</div>
	</body>
</html>